
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 2
 * @Author : WangRuoyu 
 * @Date : 2023/5/19 9:16
 */

#include "iostream"

using namespace std;

int main() {
    int m, n;
    scanf("%d %d", &m, &n);
    int pre[n + 1];
    int next[n + 1];
    int d[n + 1];

    for (int i = 0; i <= n; ++i) {
        pre[i] = i;
        next[i] = i;
        d[i] = 0;
    }

    for (int i = 1; i <= n; ++i) {
        int p;
        scanf("%d", &p);
        if (p != 0) {
            pre[i] = p;
            next[p] = i;
        }
    }

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &d[i]);
    }

    bool flag = true;
    string early;
    string late;
    for (int i = 1; i <= n; ++i) {
        int a = 0;
        int b = 0;
        for (int j = i; j != pre[j]; j = pre[j]) {
            a += d[pre[j]];
        }
        for (int j = i; j != next[j]; j = next[j]) {
            b += d[next[j]];
        }
        if (a + b + d[i] > m) flag = false;
        early += to_string(a + 1) + " ";
        late += to_string(m - d[i] - b + 1) + " ";
    }

    cout << early << endl;
    if (flag) {
        cout << late;
    }

    return 0;
}